package cn.itsource.org.controller;

import cn.itsource.basic.PageList;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.org.domain.Department;
import cn.itsource.org.query.DepartMentQuery;
import cn.itsource.org.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RestController//复核注解
/**
 * @GetMapping 获取数据
 * @PostMapping 分页高级查询
 * @PutMapping 新增或修改数据
 * @DeleteMapping 删除数据
 */

@RequestMapping("/dept")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;
   @GetMapping("/{id}")
    public Department queryOne(@PathVariable Long id){
        return departmentService.queryById(id);
    }

    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Department department) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            if (department.getId() != null) {
                departmentService.update(department);
            } else {
                departmentService.add(department);
            }
            return ajaxResult;
        } catch (Exception e) {
           e.printStackTrace();
           ajaxResult.setSuccess(false);
           ajaxResult.setMsg("操作成功");
           return ajaxResult;
        }
    }

   @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id){
        AjaxResult ajaxResult = new AjaxResult();
       try {
           departmentService.delete(id);
           return ajaxResult;
       } catch (Exception e) {
         e.printStackTrace();
         ajaxResult.setSuccess(false);
         ajaxResult.setMsg("操作成功");
         return ajaxResult;
       }
   }
    @PostMapping
   public PageList<Department> queryPage(@PathVariable DepartMentQuery query){
       return departmentService.pageLoad(query);
   }
}
